package com.example.oasystem.service.impl;

import com.example.oasystem.dao.mail.AoaInMailListDaoOfSun;
import com.example.oasystem.entity.mail.AoaInMailListOfSun;
import com.example.oasystem.entity.mail.AoaMailReciverOfSun;
import com.example.oasystem.entity.mail.AoaMailnumberOfl;
import com.example.oasystem.service.AoaInMailListServiceOfSun;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class AoaInMailListServiceImplOfSun implements AoaInMailListServiceOfSun {
    @Autowired
    private AoaInMailListDaoOfSun aoaInMailListDao;


    @Override
    public PageInfo<AoaInMailListOfSun> selectInboxNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailListOfSuns =aoaInMailListDao.selectInboxNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailListOfSuns);
        return pageinfo;
    }

    @Override
    public PageInfo<AoaInMailListOfSun> selectOutboxNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailListOfSuns =aoaInMailListDao.selectOutboxNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailListOfSuns);
        return pageinfo;
    }

    @Override
    public PageInfo<AoaInMailListOfSun> selectDraftBoxNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailListOfSuns =aoaInMailListDao.selectDraftBoxNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailListOfSuns);
        return pageinfo;
    }

    @Override
    public PageInfo<AoaInMailListOfSun> selectMailTrashCanNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailListOfSuns =aoaInMailListDao.selectMailTrashCanNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailListOfSuns);
        return pageinfo;
    }

    @Override
    public PageInfo<AoaInMailListOfSun> selectReciverTrashCanNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailListOfSuns =aoaInMailListDao.selectReciverTrashCanNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailListOfSuns);
        return pageinfo;
    }

    @Override
    public PageInfo<AoaInMailListOfSun> selectMailReciverTrashCanNum(int pageNum, int pageSize, String baseKey, long userId) {
        PageHelper.startPage(pageNum, pageSize);
        List<AoaInMailListOfSun> aoaInMailList =aoaInMailListDao.selectMailTrashCanNum(baseKey,userId);
        PageInfo<AoaInMailListOfSun> pageinfo = new PageInfo<AoaInMailListOfSun>(aoaInMailList);
        return pageinfo;
    }

    @Override
    public AoaInMailListOfSun selectOneMail(long id) {
        return aoaInMailListDao.selectOneMail(id);
    }

    @Override
    public List<AoaMailnumberOfl> selectMailNum(long userId) {
        return aoaInMailListDao.selectMailNum(userId);
    }

    @Override
    public long addMail(AoaInMailListOfSun aoaInMailListOfSun) {
        aoaInMailListDao.addMail(aoaInMailListOfSun);
        Long mailId = aoaInMailListOfSun.getMailId();
        return mailId;
    }

    @Override
    public void addMailNum(AoaMailReciverOfSun aoaMailnumberOfSun) {
        aoaInMailListDao.addMailNum(aoaMailnumberOfSun);
    }


    @Override
    public void updateMailInbox(int isRead, int isStar, int isDel,List<Long> ids,long userId) {
        aoaInMailListDao.updateMailInbox(isRead,isStar,isDel,ids,userId);
    }

    @Override
    public void updateMailOutbox(int mailStar, int mailDel, int mailPush,List<Long> ids) {
        aoaInMailListDao.updateMailOutbox(mailStar,mailDel,mailPush,ids);
    }

    @Transactional
    @Override
    public void deleteMail(long userId,List<Long> ids) {
        List<AoaInMailListOfSun> aoaInMailListOfSuns = aoaInMailListDao.selectMailTrashCanNum("", userId);
        List<Long> a = new ArrayList<>();
        System.out.println("ids = " + ids);
        for (AoaInMailListOfSun mailListOfSun : aoaInMailListOfSuns) {
            Long mailId = mailListOfSun.getMailId();
            for (int i = 0; i < ids.size(); i++) {
                if (mailId.equals(ids.get(i))){
                    a.add(ids.get(i));
                    ids.remove(i);
                }
            }
        }
        if (a.size()>0){
            aoaInMailListDao.deleteReciver(0,a);
            aoaInMailListDao.deleteMail(userId,a);
        }
        if (ids.size()>0){
            aoaInMailListDao.deleteReciver(userId,ids);
        }
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaa = " + a);
        System.out.println("ids ssssssssssssssssssssssss = " + ids);


    }

    @Override
    public void updateReduction(long userId, List<Long> ids) {
        List<AoaInMailListOfSun> aoaInMailListOfSuns = aoaInMailListDao.selectMailTrashCanNum("", userId);
        List<Long> a = new ArrayList<>();
        System.out.println("ids = " + ids);
        for (AoaInMailListOfSun mailListOfSun : aoaInMailListOfSuns) {
            Long mailId = mailListOfSun.getMailId();
            for (int i = 0; i < ids.size(); i++) {
                if (mailId.equals(ids.get(i))){
                    a.add(ids.get(i));
                    ids.remove(i);
                }
            }
        }
        if (a.size()>0){
            aoaInMailListDao.updateMailOutbox(9,0,9,a);
        }
        if (ids.size()>0){
            aoaInMailListDao.updateMailInbox(9,9,0,ids,userId);

        }
    }

    @Override
    public void updateMail(AoaInMailListOfSun mail) {
        aoaInMailListDao.updateMail(mail);
    }

    @Override
    public void deleteMailDraftbox(Long userId, List<Long> a) {
        aoaInMailListDao.deleteMail(userId,a);

    }


}
